package cn.ito.zjgsu.domain.wrapper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.ito.zjgsu.domain.Person;
import cn.ito.zjgsu.domain.UserLogin;

public class PersonWrapper {
	
	public static final boolean PERSON_EXIST_FLAG = true;
	public static final boolean PERSON_DELETE_FLAG = false;
	
	private Person person;
	private List<UserLogin> accounts; //一个员工可以有多个账号
	

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}
	
	public PersonWrapper(){}
	public PersonWrapper(Person person){
		this.person = person;
	}
	

	
	public String toJsonObjectString(){	
		return toJsonObject().toJSONString();
	}
	public JSONObject toJsonObject(){
		JSONObject json = new JSONObject();	
		initUserField(json);		
		return json;
	}

	private void initUserField(JSONObject json) {
		json.put("birthday", person.getBirthday());
		json.put("gender", person.getGender());
		json.put("createTime", person.getPersonCreateTime());
		json.put("id", person.getPersonId());
		json.put("name", person.getPersonName());
		json.put("phone", person.getPersonPhone());
		json.put("position", person.getPosition());
		json.put("positionalTitles", person.getPositionaltitles());
		if(null != accounts){
			json.put("accounts",UserWrapper.toJsonArray((UserWrapper.wrapUserLogin(accounts))));
		}
	}
	

	public static JSONArray toJsonArray(List<PersonWrapper> personWrappers){
		JSONArray array = new JSONArray();
		for(PersonWrapper pw : personWrappers){
			array.add(pw.toJsonObject());
		}
		return array;
	}
	
	public static String toJsonArrayString(JSONArray array){
		return array.toJSONString();
	}
	public static String toJsonArrayString(List<PersonWrapper> personWrappers){
		return toJsonArray(personWrappers).toJSONString();
	}
	
	public static List<PersonWrapper> wrapPersonList(List<Person> persons){
		List<PersonWrapper> pws = new ArrayList<PersonWrapper>();
		if(null != persons){		
			for(Person p : persons){
				pws.add(new PersonWrapper(p));
			}
			return pws;
		}else{
			return pws;
		}
		
	}
	
	public static Person getTestEntry(){
		Person test = new Person();
		test.setBirthday(new Date());
		test.setGender("男");
		test.setPersonCreateTime(new Date());
		test.setPersonId(1);
		test.setPersonName("cyf");
		test.setPersonPhone("17766666666");
		test.setPosition("教授");
		test.setPositionaltitles("教师");	
		return test;
	}
	
	public static PersonWrapper getTestWrapperEntry(){
		return new PersonWrapper(getTestEntry());
	}
	
	public static List<PersonWrapper> getTestListWrapper(){
		PersonWrapper pw1 = getTestWrapperEntry();
		Person p1 = getTestEntry();
		p1.setPersonId(2);
		PersonWrapper pw2 = new PersonWrapper(p1);
		List<PersonWrapper> pws = new ArrayList<PersonWrapper>(Arrays.asList(new PersonWrapper[]{
				pw1,pw2
		}));
		return pws;
	}
	
	public static boolean contains(List<Person> ps, Person p1){
		for(Person p : ps){
			if(p.getPersonId() == p1.getPersonId()){
				return true;
			}
		}
		return false;
	}
	
	public static  List<Person> removeAll(List<Person> p1 , List<Person> p2){
		for(Person p : p2){
			if(contains(p1,p)){
				remove(p1,p);
			}
		}
		return p1;
	}
	
	public static boolean remove(List<Person> ps , Person p){
		for(Person p1: ps){
			if(p1.getPersonId() == p.getPersonId()){
				ps.remove(p1);
				return true;
			}
		}
		return false;
	}

}
